package com.cheers.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.cheers.dao.ProductDao;
import com.cheers.model.Product;
import com.cheers.mvo.ProductLog;

public class ProductDaoImpl extends AbstractDaoImpl implements ProductDao {

	@Override
	public List<Product> recd() throws Exception {
		String sql = "select * from product_info where seq<>0 and status<>2 order by seq";
		List<Product> products = findBySql(Product.class, 0, 100, sql, null);
		return products;
	}
	
	@Override
	public List<Product> list(int firstIndex, int pageSize) throws Exception {
		String sql = "select * from product_info where status<>2 order by ctime desc";
		List<Product> products = findBySql(Product.class, firstIndex, pageSize, sql, null);
		return products;
	}

	@Override
	public List<ProductLog> exchangeList(String userId, int firstIndex, int pageSize) throws Exception {
		List<ProductLog> result = null;
		String sql = "select info.id,info.title,info.price,info.picture,info.depict, log.status,log.point,log.num,log.ctime from exchangeproduct_log log, product_info info where log.userId=? and log.productId=info.id order by ctime desc";
		Object[] params = new Object[]{userId};
		List<Object[]> productLogs = findBySql(firstIndex, pageSize, sql, params);
		if (productLogs!=null && !productLogs.isEmpty()) {
			result = new ArrayList<ProductLog>();
			ProductLog productLog = null;
			for (Object[] obj : productLogs) {
				productLog = new ProductLog();
				productLog.setProductId(String.valueOf(obj[0]));
				productLog.setTitle(String.valueOf(obj[1]));
				productLog.setPrice(String.valueOf(obj[2]));
				productLog.setPicture(String.valueOf(obj[3]));
				productLog.setDescribe(String.valueOf(obj[4]));
				productLog.setStatus(String.valueOf(obj[5]));
				productLog.setPoint(String.valueOf(obj[6]));
				productLog.setNumber(String.valueOf(obj[7]));
				productLog.setCtime(sdf.format(obj[8]));
				
				result.add(productLog);
			}
		}
		return result;
	}
	
}
